﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
//using System.Diagnostics;

namespace Royaume
{
    public partial class ucLogin : System.Web.UI.UserControl
    {
        Dictionary<SqlParameter, object> myParam = new Dictionary<SqlParameter, object>();
        cSql objSql = new cSql("(local)", "DB_USERS", "RoyaumesLibres", "123456");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["isLog"] != null)
                {
                    this.multiView.SetActiveView(vwIsLogStandard);
                    this.lblConnectedStandard.Text = "Bienvenue " + Session["nomUser"].ToString(); 
                }
                else
                {
                    this.multiView.SetActiveView(vwLoginStandard);
                    this.txtLoginStandard.Height = 20;
                    this.txtPassStandard.Height = 20;
                    this.txtLoginStandard.Width = 155;
                    this.txtPassStandard.Width = 155;
                }
            }
            else
            {
                if (Session["isLog"] != null)
                {
                    this.multiView.SetActiveView(vwIsLogStandard);
                    //this.lblConnectedStandard.Text = "Bienvenue " + Session["pkUser"].ToString();
                }
            }
        }

        protected void btnLoginStandard_Click(object sender, EventArgs e)
        {
            try
            {
                string sUserPass = cUtility.Encrypt(this.txtPassStandard.Text);
                string sUserLog = this.txtLoginStandard.Text;
                myParam.Clear();
                myParam.Add(new SqlParameter("@Login", SqlDbType.VarChar, 16), sUserLog);
                myParam.Add(new SqlParameter("@Password", SqlDbType.VarChar, 255), sUserPass);

                int iIsLog = objSql.fnExecScalarInt("spLogin", myParam);
                if (iIsLog == 1)
                {
                    Session["isLog"] = true;
                    myParam.Clear();
                    myParam.Add(new SqlParameter("@login", SqlDbType.VarChar, 16), sUserLog);
                    string sMembreInfo = objSql.fnExecSalarString("spGetPkMembre", myParam);
                    string[] info = sMembreInfo.Split(';');
                   

                    Session.Add("pkUser", info[0]);
                    Session.Add("nomUser", info[1]);
                    
                    this.multiView.SetActiveView(vwIsLogStandard);

                    this.lblConnectedStandard.Text = "Bienvenue " + Session["nomUser"].ToString();
                    //cUtility.fnMsgBox(Session["nomUser"].ToString(), Page);
                    Response.Redirect("map.aspx");
                }

            }
            catch (Exception ex)
            {
                //cUtility.fnMsgBox("Nous n'avons pas pu vous connecter", Page, this);
            }
        }
    }
}